package com.amphora.web.actions;

import java.io.IOException;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.amphora.model.Client;
import com.amphora.services.application.ClientServices;
import com.amphora.services.application.impl.ClientServiceImpl;
import com.amphora.util.repository.JPAUtil;

@WebServlet("/ClientAction")
public class ClientAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static String LOGIN_SUCCESS = "/amphora_home.jsp";
	private static String LOGOUT = "/index.jsp";
	private static String LOGIN_ERROR = "/index.jsp";
	private static String ERROR_ = "/error.jsp";
	private ClientServices cs;
	
	public void init(ServletConfig config) throws ServletException {
		System.out.println("ClientAction init method");
		cs = new ClientServiceImpl();
	}
	public void destroy() {
		System.out.println("ClientAction destroy method");
	}
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("ClientAction get method");
		HttpSession session = request.getSession();
		Map parameters = request.getParameterMap();
		String forward = "";
		
		if(parameters.containsKey("view")){
			System.out.println("ClientAction: amphora home view page");
			forward = LOGIN_SUCCESS;
		}else if(parameters.containsKey("des")){
			System.out.println("ClientAction: logout");
			session.invalidate();
			forward = LOGOUT;
		}else{
			forward = ERROR_;
		}
		
		RequestDispatcher view = request.getRequestDispatcher(forward);
		view.forward(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("ClientAction post method");
		HttpSession session = request.getSession();
		
		Map parameters = request.getParameterMap();
		String forward = "";
		
		if(parameters.containsKey("lg")){
			String username = request.getParameter("username");
			String password = request.getParameter("password");
			
			if((!username.equals("")&&username!=null)&&(!password.equals("")&&password!=null)){
				if(cs.loginClient(username, password)!=null){
					Client loggedIn = cs.loginClient(username, password);
					System.out.println("ClientAction: client logged in successfully");
					session.setAttribute("loginID", loggedIn.getId());
					request.setAttribute("clientName", loggedIn.getName());
					forward = LOGIN_SUCCESS;
				}else{
					System.out.println("ClientAction: client login unsuccessful");
					request.setAttribute("message", "ClientAction: client login unsuccessful, client is not registered");
					forward = LOGIN_ERROR;
				}
			}else{
				request.setAttribute("message", "ClientAction: client login unsuccessful, please ensure correct details are entered");
			}
		}else{
			forward = ERROR_;
		}
		
		RequestDispatcher view = request.getRequestDispatcher(forward);
		view.forward(request,response);
	}

}
